clear all
set more off

do "set_path.do"

***** Fig 8: Event Plots *****

local curr_dir `c(pwd)'
cd ${tempdir}

use "${datadir}cip_all_both.dta", clear
keep if y_usd!=.
collapse (lastnm) y_usd, by(date)
bcal create "${tempdir}bus_date", from(date) replace

use "${datadir}cip_all_both.dta", clear

keep if y_usd!=.
gen bus_date=bofd("bus_date",date)
format bus_date %tbbus_date

gen cip_rf=(r-r_usd-rho)*100
gen sofr_cip_rf=(sofr_r-sofr_r_usd-sofr_rho)*100

keep group currency tenor bus_date cip_govt cip_rf sofr_cip_rf

reshape wide cip_govt cip_rf sofr_cip_rf, i(group tenor bus_date) j(currency) string
reshape long cip_govt cip_rf sofr_cip_rf, i(group tenor bus_date) j(currency) string
sort group currency tenor bus_date
bys group currency tenor: replace cip_govt = cip_govt[_n-1] if missing(cip_govt) & !missing(cip_govt[_n-1])
bys group currency tenor: replace cip_rf = cip_rf[_n-1] if missing(cip_rf) & !missing(cip_rf[_n-1])
bys group currency tenor: replace sofr_cip_rf = sofr_cip_rf[_n-1] if missing(sofr_cip_rf) & !missing(sofr_cip_rf[_n-1])

save ${tempdir}cip_event.dta, replace

use ${tempdir}cip_event.dta, clear

collapse (mean) cip_govt cip_rf sofr_cip_rf, by(group bus_date tenor)

*** Tarriff
local w_start = bofd("bus_date",td(26mar2025))
local w_mid = bofd("bus_date",td(02apr2025))
local w_end = bofd("bus_date",td(09apr2025))
local w_start2 = `w_start'-2
local w_end2 = `w_end'+2

twoway (connected cip_govt bus_date if tenor=="3m") ///
       (connected cip_govt bus_date if tenor=="1y") ///
       (connected cip_govt bus_date if tenor=="5y") ///
       (connected cip_govt bus_date if tenor=="10y") ///
	   if group=="g10" & inrange(bus_date, `w_start2', `w_end2'), ///
       xline(`w_mid', lpattern(dash) lcolor(black)) ///
       xscale(range(`w_start2' `w_end2')) ///
       legend(order(1 "3 Month" 2 "1 Year" 3 "5 Year" 4 "10 Year") rows(1)) ///
       ytitle("Basis Points") xtitle("") ///
       xlabel(`w_start' "26mar2025" `w_mid' "02apr2025"  `w_end' "09apr2025", nogrid)  ///
	   ylabel(, nogrid) ///
       legend(row(1) pos(6)) ///
	   xsize(6) ysize(4)
graph export "${outdir}figures/tariffs_event_cip_govt.eps", replace

twoway (connected cip_rf bus_date if tenor=="3m") ///
       (connected cip_rf bus_date if tenor=="1y") ///
       (connected cip_rf bus_date if tenor=="5y") ///
       (connected cip_rf bus_date if tenor=="10y") ///
	   if group=="g10" & inrange(bus_date, `w_start2', `w_end2'), ///
       xline(`w_mid', lpattern(dash) lcolor(black)) ///
       xscale(range(`w_start2' `w_end2')) ///
       legend(order(1 "3 Month" 2 "1 Year" 3 "5 Year" 4 "10 Year") rows(1)) ///
       ytitle("Basis Points") xtitle("") ///
       xlabel(`w_start' "26mar2025" `w_mid' "02apr2025"  `w_end' "09apr2025", nogrid)  ///
	   ylabel(, nogrid) ///
       legend(row(1) pos(6)) ///
	   xsize(6) ysize(4)
graph export "${outdir}figures/tariffs_event_cip_rf.eps", replace
		
*** Lehman
local w_start = bofd("bus_date",td(08sep2008))
local w_mid = bofd("bus_date",td(15sep2008))
local w_end = bofd("bus_date",td(22sep2008))
local w_start2 = `w_start'-2
local w_end2 = `w_end'+2

twoway (connected cip_govt bus_date if tenor=="3m") ///
       (connected cip_govt bus_date if tenor=="1y") ///
       (connected cip_govt bus_date if tenor=="5y") ///
       (connected cip_govt bus_date if tenor=="10y") ///
	   if group=="g10" & inrange(bus_date, `w_start2', `w_end2'), ///
       xline(`w_mid', lpattern(dash) lcolor(black)) ///
       xscale(range(`w_start2' `w_end2')) ///
       legend(order(1 "3 Month" 2 "1 Year" 3 "5 Year" 4 "10 Year") rows(1)) ///
       ytitle("Basis Points") xtitle("") ///
       xlabel(`w_start' "08sep2008" `w_mid' "15sep2008"  `w_end' "22sep2008", nogrid)  ///
	   ylabel(, nogrid) ///
       legend(row(1) pos(6)) ///
	   xsize(6) ysize(4)
graph export "${outdir}figures/lehman_event_cip_govt.eps", replace
		
twoway (connected cip_rf bus_date if tenor=="3m") ///
       (connected cip_rf bus_date if tenor=="1y") ///
       (connected cip_rf bus_date if tenor=="5y") ///
       (connected cip_rf bus_date if tenor=="10y") ///
	   if group=="g10" & inrange(bus_date, `w_start2', `w_end2'), ///
       xline(`w_mid', lpattern(dash) lcolor(black)) ///
       xscale(range(`w_start2' `w_end2')) ///
       legend(order(1 "3 Month" 2 "1 Year" 3 "5 Year" 4 "10 Year") rows(1)) ///
       ytitle("Basis Points") xtitle("") ///
       xlabel(`w_start' "08sep2008" `w_mid' "15sep2008"  `w_end' "22sep2008", nogrid)  ///
	   ylabel(, nogrid) ///
       legend(row(1) pos(6)) ///
	   xsize(6) ysize(4) 
graph export "${outdir}figures/lehman_event_cip_rf.eps", replace

*** Covid
local w_start = bofd("bus_date",td(02mar2020))
local w_mid = bofd("bus_date",td(09mar2020))
local w_end = bofd("bus_date",td(16mar2020))
local w_start2 = `w_start'-2
local w_end2 = `w_end'+2

twoway (connected cip_govt bus_date if tenor=="3m") ///
       (connected cip_govt bus_date if tenor=="1y") ///
       (connected cip_govt bus_date if tenor=="5y") ///
       (connected cip_govt bus_date if tenor=="10y") ///
       if group=="g10" & inrange(bus_date, `w_start2', `w_end2'), ///
       xline(`w_mid', lpattern(dash) lcolor(black)) ///
       xscale(range(`w_start2' `w_end2')) ///
       legend(order(1 "3 Month" 2 "1 Year" 3 "5 Year" 4 "10 Year") rows(1)) ///
       ytitle("Basis Points") xtitle("") ///
       xlabel(`w_start' "02mar2020" `w_mid' "09mar2020"  `w_end' "16mar2020", nogrid)  ///
	   ylabel(, nogrid) ///
       legend(row(1) pos(6)) ///
	   xsize(6) ysize(4) 
graph export "${outdir}figures/covid_event_cip_govt.eps", replace

twoway (connected cip_rf bus_date if tenor=="3m") ///
       (connected cip_rf bus_date if tenor=="1y") ///
       (connected cip_rf bus_date if tenor=="5y") ///
       (connected cip_rf bus_date if tenor=="10y") ///
	   (connected sofr_cip_rf bus_date if tenor=="3m", lpattern(dash)) ///
       if group=="g10" & inrange(bus_date, `w_start2', `w_end2'), ///
       xline(`w_mid', lpattern(dash) lcolor(black)) ///
       xscale(range(`w_start2' `w_end2')) ///
       legend(order(1 "3 Month" 2 "1 Year" 3 "5 Year" 4 "10 Year" 5 "3 Month (SOFR)") rows(1)) ///
       ytitle("Basis Points") xtitle("") ///
       xlabel(`w_start' "02mar2020" `w_mid' "09mar2020"  `w_end' "16mar2020", nogrid)  ///
	   ylabel(, nogrid) ///
       legend(row(1) pos(6)) ///
	   xsize(6) ysize(4) 
graph export "${outdir}figures/covid_event_cip_rf.eps", replace

cd "`curr_dir'"
